﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BF.IBLL.Dictionary;
using Base.Core.UnityFactory;
using BF.Model;
using System.Web.Script.Serialization;

namespace BF.Web.pages.Dictionary.HospitalManagement
{
    public class NodeInfo
    {
        //定义jsondata类，存放节点数据
        public string gKey; //gKey; //主键
        public string text;  //机构名称
        public bool leaf;
        public string cls;
        public string icon;
        public string iconCls;
        public bool expanded;
        //public string sAreaCode;
        //public bool singleClickExpand;
        //public bool Checked;
        public string gHigherKey;  //父节点
        public List<NodeInfo> children = new List<NodeInfo>();//存放子节点
    }

    /// <summary>
    /// HHospitalTree 的摘要说明
    /// </summary>
    public class HHospitalTree : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string gKey = context.Request.QueryString["gKey"] ?? default(Guid).ToString();
            string gFatherKey = context.Request.QueryString["gFatherKey"] ?? default(Guid).ToString();
            Idic_OrganizationBLL iBLL = IOCFactory.GetIOCResolve<Idic_OrganizationBLL>();
            List<dic_Organization> allList = iBLL.GetList(p => p.iUse == 1);
            if (gFatherKey == default(Guid).ToString())
            {
                dic_Organization benjigou = allList.FirstOrDefault(p => p.gKey == new Guid(gKey));
                NodeInfo rootNode = new NodeInfo();
                rootNode.gKey = benjigou.gKey.ToString();
                rootNode.text = benjigou.sOrganizationName;
                rootNode.icon = "/images/icons/house.png";
                rootNode.expanded = true;
                rootNode.gHigherKey = benjigou.gHigherKey.ToString();
                LoadNodes(rootNode, allList);
                List<NodeInfo> jsdata = new List<NodeInfo>();
                jsdata.Add(rootNode);
                string strJson = ToJson(jsdata.ToArray()).Replace("Checked", "checked");
                strJson = strJson
                    .Replace("\"\\u003c\\u003c", "{")
                    .Replace("\\u003e\\u003e\"", "}")
                    .Replace("\\u0027", "")
                    .Replace("\\\"", "'")
                    ;
                context.Response.Write(strJson);
            }
            else
            {
                List<dic_Organization> list = allList.Where(p => p.gHigherKey == new Guid(gFatherKey)).
                    OrderBy(P => P.sOrganizationType).ThenBy(P => P.sOrganizationCode).
                    ToList();
                List<NodeInfo> jsdata = new List<NodeInfo>();
                foreach (dic_Organization item in list)
                {
                    NodeInfo node = new NodeInfo();
                    node.gKey = item.gKey.ToString();
                    node.text = item.sOrganizationName;
                    node.icon = "/images/icons/house.png";
                    node.expanded = false;
                    node.gHigherKey = item.gHigherKey.ToString();
                    LoadNodes(node, allList);
                    jsdata.Add(node);
                }
                string strJson = ToJson(jsdata.ToArray()).Replace("Checked", "checked");
                strJson = strJson
                    .Replace("\"\\u003c\\u003c", "{")
                    .Replace("\\u003e\\u003e\"", "}")
                    .Replace("\\u0027", "")
                    .Replace("\\\"", "'")
                    ;
                context.Response.Write(strJson);
            }
        }

        private void LoadNodes(NodeInfo parentNode, List<dic_Organization> allList)
        {
            List<dic_Organization> currentList = allList
       .Where(P => P.gHigherKey.Equals(new Guid(parentNode.gKey)))
       .OrderBy(P => P.sOrganizationType).ThenBy(P => P.sOrganizationCode)
       .ToList();
            parentNode.leaf = currentList.Count == 0;
            foreach (dic_Organization item in currentList)
            {
                NodeInfo node = new NodeInfo();
                node.gKey = item.gKey.ToString();
                node.text = item.sOrganizationName;
                node.icon = "/images/icons/house.png";
                node.expanded = false;
                node.gHigherKey = item.gHigherKey.ToString();
                parentNode.children.Add(node);
                LoadNodes(node, allList);
            }
        }

        public string ToJson(object o)
        {
            //序列化对象为json数据，很重要！
            JavaScriptSerializer j = new JavaScriptSerializer();
            return j.Serialize(o);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}